package com.yanq.liancar.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.queryDownPayAfterMsgBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.QRCodeUtil;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PurchaseSuccessActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_shopname)
    TextView tvShopname;
    @BindView(R.id.tv_Reserve)
    TextView tvReserve;
    @BindView(R.id.tv_shopname2)
    TextView tvShopname2;
    @BindView(R.id.tv_details)
    TextView tvDetails;
    @BindView(R.id.tv_details2)
    TextView tvDetails2;
    @BindView(R.id.tv_right_go)
    ImageView tvRightGo;
    @BindView(R.id.img_QRcode)
    ImageView imgQRcode;
    @BindView(R.id.tv_juanNum)
    TextView tvJuanNum;

    private String orderID;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchasesuccess);
        Utils.setStatusBar(this, true,false);
        ButterKnife.bind(this);
        mTvTitle.setText("购买成功");

        orderID=getIntent().getExtras().getString("orderID");
        LogUtils.e("2313131",orderID+"");
        DiscountBuy();
    }

    @OnClick({R.id.tv_Reserve, R.id.tv_details2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_Reserve:
                break;
            case R.id.tv_details2:
                break;
        }
    }

    //买单接口
    private void DiscountBuy(){
        OkGo.<String>post(Config.s + "llc/queryDownPayAfterMsg")
                .tag(this)
                .params("onum",orderID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        queryDownPayAfterMsgBean baseBean=gson.fromJson(response.body(),queryDownPayAfterMsgBean.class);
                        if (baseBean.isIsSuccess()){
                            if (baseBean.getData().getServiceType()==80||baseBean.getData().getServiceType()==90){
                                tvDetails.setText("精致洗车");
                            }else if (baseBean.getData().getServiceType()==2){
                                tvDetails.setText("标准洗车");
                            }else if (baseBean.getData().getServiceType()==4){
                                tvDetails.setText("保养维修");
                            }else if (baseBean.getData().getServiceType()==6){
                                tvDetails.setText("代办服务");
                            }else if (baseBean.getData().getServiceType()==3){
                                tvDetails.setText("美容养护");
                            }
                            tvShopname.setText(baseBean.getData().getShopName());
                            tvShopname2.setText(baseBean.getData().getShopName());
                            tvJuanNum.setText("券号："+baseBean.getData().getOnum());
                            Bitmap bitmap= QRCodeUtil.createQRCodeBitmap("OrderDetails"+baseBean.getData().getOnum(),dp2px(PurchaseSuccessActivity.this, 150),dp2px(PurchaseSuccessActivity.this, 150));
                            imgQRcode.setImageBitmap(bitmap);
                            tvJuanNum.setText("券号："+baseBean.getData().getOnum());
                        }else {
                            RxToast.error(baseBean.getOutputMessage());
                        }
                    }
                });
    }

    //dp转px
    public static int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
                context.getResources().getDisplayMetrics());
    }
}
